NVM.Animations = function () {
    var __core = NOVEM.Core;

    return {
        init: function (selector) {
            if (__core.getSelector(selector, false, false).length < 1) {
                return true;
            }

            __core.initFunction({ class: 'has-plugin-animations', event: 'pluginAnimationsReady' });

            selector = __core.getSelector(selector, false);
            if (selector.length < 1) {
                return true;
            }

            var SELECTOR = '[data-animate]',
                ANIMATE_CLASS_NAME = 'animate__animated';

            // var isAnimated = function (element) {
            //     element.classList.contains(ANIMATE_CLASS_NAME);
            // };

            var intersectionObserver = new IntersectionObserver(
                function (entries, observer) {
                    entries.forEach(function (entry) {
                        var element = entry.target,
                            elAnimation = element.getAttribute('data-animate'),
                            elAnimOut = element.getAttribute('data-animate-out'),
                            elAnimDelay = element.getAttribute('data-delay'),
                            elAnimDelayOut = element.getAttribute('data-delay-out'),
                            elAnimDelayTime = 0,
                            elAnimDelayOutTime = 3000,
                            elAnimations = elAnimation.split(' ');
                        if (element.closest('.swiper-slide')) {
                            return true;
                        }

                        if (elAnimDelay) {
                            elAnimDelayTime = Number(elAnimDelay) + 500;
                        } else {
                            elAnimDelayTime = 500;
                        }

                        if (elAnimOut && elAnimDelayOut) {
                            elAnimDelayOutTime = Number(elAnimDelayOut) + elAnimDelayTime;
                        }

                        if (entry.intersectionRatio > 0) {
                            // console.log("进入");
                            new Promise((resolve, reject) => {
                                elAnimations.forEach(function (item) {
                                    element.classList.add(item);
                                });

                                element.classList.add(ANIMATE_CLASS_NAME);

                                function handleAnimationEnd(event) {
                                    event.stopPropagation();
                                    elAnimations.forEach(function (item) {
                                        element.classList.remove(item);
                                    });
                                    element.classList.remove(ANIMATE_CLASS_NAME);
                                    resolve('Animation ended');
                                }

                                element.addEventListener('animationend', handleAnimationEnd, { once: true });
                            });

                            if (elAnimOut) {
                                setTimeout(function () {
                                    elAnimations.forEach(function (item) {
                                        element.classList.remove(item);
                                    });

                                    elAnimOut.split(' ').forEach(function (item) {
                                        element.classList.add(item);
                                    });
                                }, elAnimDelayOutTime);
                            }
                        }
                        // else {
                        //     console.log("移出");
                        // }

                        // if (!element.classList.contains('not-animated')) {
                        //     observer.unobserve(element);
                        // }
                    });

                }
            );

            // var elements = [].filter.call(document.querySelectorAll(SELECTOR), function (element) {
            //     return !isAnimated(element, ANIMATE_CLASS_NAME);
            // });

            var elements = document.querySelectorAll(SELECTOR);

            elements.forEach(function (element) {
                return intersectionObserver.observe(element);
            });
        }
    };
}();
